package smlab.loader
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.system.ApplicationDomain;
	import flash.system.SecurityDomain;
	
	import smlab.events.LoadListEvent_SM;
	
	/**
	 * 整合了 LoadList（队列加载） 和 LoadingPanel（loading 界面）
	 * @author simon
	 * 
	 */
	public class LoaderMix extends EventDispatcher
	{
		private var _loadList:LoadList_SM;
		private var _loadingPanel:LoadingPanel_SM;
		private var _parentMC:DisplayObjectContainer;
		
		public function LoaderMix(url:String,loadingSkin$:Sprite=null,parentMC$:DisplayObjectContainer=null,defaultMsg$:String="",isShowLoading$:Boolean=true,checkPolicyFile:Boolean = false, applicationDomain:ApplicationDomain = null, securityDomain:SecurityDomain = null)
		{
			this.parentMC=parentMC$;
			
			if(loadingSkin$){
				_loadingPanel=new LoadingPanel_SM(loadingSkin$);
				
				if(parentMC$){
					parentMC$.addChild(_loadingPanel);
				}
			}
			
			_loadList=new LoadList_SM(_loadingPanel,defaultMsg$,checkPolicyFile, applicationDomain, securityDomain);
			_loadList.addEventListener(LoadListEvent_SM.COMPLETE,onComplete);
			_loadList.addEventListener(LoadListEvent_SM.ERROR,onError);
			_loadList.push(url);
			
			if(isShowLoading$){
				if(loadingSkin$){
					_loadList.loadingSkin.showLoading();
				}
			}else{
				if(loadingSkin$){
					_loadList.loadingSkin.isShowLoading=false;
				}
			}
		}
		
		public function get loadList():LoadList_SM
		{
			return _loadList;
		}
		
		public function set parentMC(d:DisplayObjectContainer):void
		{
			_parentMC=d;
		}
		
		public function get parentMC():DisplayObjectContainer
		{
			return _parentMC;
		}
		
		public function close():void
		{
			if(_parentMC){
				if(_loadingPanel){
					if(_parentMC.contains(_loadingPanel)){
						_parentMC.removeChild(_loadingPanel);
					}
					_loadingPanel=null;
				}
				_parentMC=null;
			}
			
			if(_loadList){
				_loadList.removeEventListener(LoadListEvent_SM.COMPLETE,onComplete);
				_loadList.removeEventListener(LoadListEvent_SM.ERROR,onError);
				_loadList.close();
				_loadList=null;
			}
		}
		
		public function start():void
		{
			_loadList.start();
		}
		
		private function onComplete(e:LoadListEvent_SM):void
		{
			this.dispatchEvent(new LoadListEvent_SM(LoadListEvent_SM.COMPLETE));
		}
		
		private function onError(e:LoadListEvent_SM):void
		{
			this.dispatchEvent(new LoadListEvent_SM(LoadListEvent_SM.ERROR));
		}
	}
}